package com.ldcc.report_system.util.http;

import com.ldcc.report_system.api.AppVersionApi;
import com.ldcc.report_system.api.DayReportApi;
import com.ldcc.report_system.api.UserApi;
import com.ldcc.report_system.api.WeekReportApi;
import com.ldcc.report_system.util.ServerUtil;
import com.ldcc.report_system.util.http.Interceptor.HeaderInterceptor;
import com.ldcc.report_system.util.http.Interceptor.LogInterceptor;
import com.ldcc.report_system.ApplicationConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/***
 * @author wwj[yeshengkele@outlook.com]
 * @description 请求对象管理类
 */
public class ClientRequestManage {
    //初始化Okhttp,绑定拦截器事件
    private OkHttpClient client;
    private Retrofit retrofit;
    //----------通过retrofit的实例,获取各个服务接口接口的--------------
    private UserApi userApi;
    private DayReportApi dayReportApi;
    private WeekReportApi weekReportApi;
    private AppVersionApi appVersionApi;
    //----------通过retrofit的实例,获取各个服务接口接口的--------------
    // 单例获取当前对象
    private static ClientRequestManage instance;

    /**
     * 初始化
     */
    private ClientRequestManage() {


        this.client = new OkHttpClient.Builder().
                //设置请求超时时间
                        connectTimeout(ApplicationConfig.SERVER_CONNECT_TIMEOUT, TimeUnit.SECONDS).
                //设置读取数据超时时间
                        readTimeout(ApplicationConfig.SERVER_CONNECT_TIMEOUT, TimeUnit.SECONDS).
                //设置写入数据超时时间
                        writeTimeout(ApplicationConfig.SERVER_CONNECT_TIMEOUT, TimeUnit.SECONDS).
                //绑定日志拦截器
                        addInterceptor(LogInterceptor.logInterceptor()).
                //绑定header拦截器
                        addNetworkInterceptor(new HeaderInterceptor())
                .build();
        this.retrofit = new Retrofit.Builder().
                //设置gson转换器,将返回的json数据转为实体
                        addConverterFactory(GsonConverterFactory.create()).
                //设置CallAdapter
                        addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                // 设置服务器地址
                        baseUrl(ServerUtil.getServerUrl()).
                //设置客户端okhttp相关参数
                        client(this.client)
                .build();

        //----------通过retrofit的实例,获取各个服务接口接口的--------------
        userApi = this.retrofit.create(UserApi.class);
        dayReportApi = this.retrofit.create(DayReportApi.class);
        weekReportApi = this.retrofit.create(WeekReportApi.class);
        appVersionApi = this.retrofit.create(AppVersionApi.class);
        //----------通过retrofit的实例,获取各个服务接口接口的--------------

    }

    public static ClientRequestManage getInstance() {
        if (ClientRequestManage.instance == null) {
            synchronized (ClientRequestManage.class) {
                if (ClientRequestManage.instance == null) {
                    ClientRequestManage.instance = new ClientRequestManage();
                }
            }
        }
        return ClientRequestManage.instance;
    }


    public UserApi getUserApi() {
        return userApi;
    }

    public DayReportApi getDayReportApi() {
        return dayReportApi;
    }

    public WeekReportApi getWeekReportApi() {
        return weekReportApi;
    }

    public AppVersionApi getAppVersionApi() {
        return appVersionApi;
    }
}
